package ggze.esp.controller;

import ggze.esp.dao.inter.FspViewSqlDao;
import ggze.esp.dao.inter.MPSDao;
import ggze.esp.entity.FspViewSql;
import ggze.loader.Storage;
import ggze.tool.ReturnBody;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
public class BspController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private FspViewSqlDao fspViewSqlDao;
    @Autowired
    private MPSDao mpsDao;
    @RequestMapping(value = "/bsp",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnBody BSP(@RequestBody JSONObject jsonObject){
        ReturnBody returnBody = new ReturnBody();
        try {
            //验证静态token
            String APPID = jsonObject.getString("APPID");
            if(!APPID.equals(Storage.getObj().getConfig().get("APPID"))){
                returnBody.setTitle("请输入正确的APPID");
                returnBody.setCode("0");
                return returnBody;
            }
            String uuid = jsonObject.getString("uuid");
            FspViewSql fspViewSql = fspViewSqlDao.isSelect("uuid",uuid);
            String sql = fspViewSql.getF_SQL();
            if(fspViewSql.getF_STATUS().equals("1")){
                returnBody.setTitle("已被禁用！");
                returnBody.setCode("1");
            }else{
                if(fspViewSql.getF_BEAN().equals("1")){
                    if(sql.indexOf("where")<0&&sql.indexOf("WHERE")<0){
                        sql+=" where ";
                    }
                    sql+=this.packSqlBane(jsonObject);
                    sql+=" order by F_METHOD_TIME desc";
                    List<Map<String, String>> list = mpsDao.commonSql(sql);
                    returnBody.setCount(list.size());
                    int begin=Integer.parseInt(jsonObject.getString("page"));
                    int end = Integer.parseInt(jsonObject.getString("limit"));
                    begin = (begin - 1) * end;
                    sql+=" limit "+begin+", "+end;
                    list = mpsDao.commonSql(sql);
                    returnBody.setCode("0");
                    returnBody.setData(list);
                }else{
                    //查询
                    if(fspViewSql.getF_TYPE().equals("4")){
                        List<Map<String, String>> list = mpsDao.commonSql(sql);
                        returnBody.setCode("0");
                        returnBody.setData(list);
                    }else if(fspViewSql.getF_TYPE().equals("5")){
                        //列名
                        returnBody.setCode("0");
                        returnBody.setData(fspViewSql.getF_SQL());
                    }else{
                        int i = mpsDao.commonSqlUp(sql);
                        returnBody.setCode("0");
                        returnBody.setData(i);
                    }
                }

            }
        }catch (Exception e){
            returnBody.setCode("1");
        }
        return returnBody;
    }
    //bean映射
    private String packSqlBane(JSONObject jsonObject){
        StringBuilder where = new StringBuilder();
        try {
            where.append(" 1=1 ");
            List<Map<String,String>> whereMAP=(List<Map<String,String>>) jsonObject.getJSONArray("common");
            //ZDNAME,ZDVAL,LIKE
            for(Map<String,String> ZD:whereMAP){
                if(ZD.get("LIKE").equals("1")){
                    where.append(" AND "+ZD.get("ZDNAME")+" = '"+ZD.get("ZDVAL")+"' ");
                }else {
                    where.append(" AND "+ZD.get("ZDNAME")+" like '%"+ZD.get("ZDVAL")+"%' ");
                }
            }
        }catch (Exception e){
            return "";
        }
        return where.toString();
    }
}
